package leaf.cloud.auth.service;

import leaf.cloud.common.core.constant.Constants;
import leaf.cloud.common.core.constant.SecurityConstants;
import leaf.cloud.common.core.utils.ServletUtils;
import leaf.cloud.common.core.utils.StrUtils;
import leaf.cloud.common.core.utils.ip.IpUtils;
import leaf.cloud.common.system.RemoteLogService;
import leaf.cloud.common.system.domain.SysLoginInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 记录日志方法
 *
 * @author 烨钟鸣
 */
@Component
public class SysRecordLogService {
    @Resource
    private RemoteLogService remoteLogService;

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status   状态
     * @param message  消息内容
     */
    public void recordLogininfor(String username, String status, String message) {
        SysLoginInfo logininfor = new SysLoginInfo();
        logininfor.setUserName(username);
        logininfor.setIpaddr(IpUtils.getIpAddr(ServletUtils.getRequest()));
        logininfor.setMsg(message);
        // 日志状态
        if (StrUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
            logininfor.setStatus(Constants.LOGIN_SUCCESS_STATUS);
        } else if (Constants.LOGIN_FAIL.equals(status)) {
            logininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
        }
        remoteLogService.loginInfo(logininfor, SecurityConstants.INNER);
    }
}
